package com.rg.servlet;

import com.rg.model.OrderModel;
import com.rg.model.UserModel;
import com.rg.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/addOrderServlet")
public class addOrderServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserModel user=(UserModel) request.getSession().getAttribute("user");
        if (user!=null){
            OrderModel order=(OrderModel) request.getSession().getAttribute("order");
            order.setDatetime(new Date());
            order.setUser(user);
//            提交订单
            OrderService orderService=new OrderService();
            orderService.saveOrder(order);
            request.getSession().removeAttribute("order");
            request.setAttribute("addOrderinfo","订单提交成功！");
            request.getRequestDispatcher("order_success.jsp").forward(request,response);
        }else {
            request.setAttribute("orderinfo","请登录后再提交订单");
            response.setHeader("refresh","3;url=login.jsp");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }
}